/*
mem 接口封装了一组内存分配函数。
**/

#ifndef _MEM_INCLUDED_
#define _MEM_INCLUDED_

#include "except.h"

extern const Except_T ex_assert_failed;

void* Mem_alloc(long nbytes, const char *file, int line);
void* Mem_calloc(long count, long nbytes, const char *file, int line);
void* Mem_resize(void *ptr, long nbytes, const char *file, int line);
void  Mem_free(void *ptr, const char *file, int line);

#define ALLOC(nbytes) \
	Mem_alloc(nbytes, __FILE__, __LINE__)
#define CALLOC(count, nbytes) \
	Mem_calloc(count, nbytes, __FILE__, __LINE__)

#define NEW(p) \
	((p) = ALLOC(sizeof(*(p))))
#define NEW0(p) \
	((p) = CALLOC(1, sizeof(*(p))))
#define RESIZE(p, nbytes) \
	((p) = Mem_resize((p), (nbytes), __FILE__, __LINE__))
#define FREE(p) \
	(void)(Mem_free(p, __FILE__, __LINE__), (p) = NULL)

#endif // _MEM_INCLUDED_
